<?php

namespace App\Controller\Common;

use App\Model\AreaModel;
use App\Model\CityModel;
use App\Model\ProvinceModel;
use App\Base\BaseConfig;
use App\Base\BaseController;
use App\Base\BaseModel;
use BaseExt\JPush;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;


/**
 * @route("/city")
 */
class CityController extends BaseController
{
    public function initData()
    {
        $request = Request::createFromGlobals();
        $url = $request->getBaseUrl();
        if (strpos($url,"pp_dev")>0) {
            $env = "dev";
            $data["rnd"] = "?rnd=".date("YmdHis");
//            $data["rnd"] = "?rnd=".date("YmdH");
            $data["asset"] = BaseConfig::__BASE_URL__;
            $data["root"] = BaseConfig::__BASE_URL__;
        } else {
            $env = "prod";
            $data["rnd"] = "?v=1.07";
            $data["asset"] = "http://sale.assets.sano100.com/ac/dc";
            $data["root"] = BaseConfig::__BASE_URL__;
        }
        $data["env"] = $env;
        $data["defaultPic"] = $data["asset"]."/assets/default.png";
        return $data;
    }

    /**
     * @route("/test",name="CityTest")
     */
    public function CityTest(){
        $ProvinceModel = new ProvinceModel($this->getDoctrine());
        $CityModel = new CityModel($this->getDoctrine());
        $AreaModel = new AreaModel($this->getDoctrine());
        $p = $ProvinceModel->getRep()->findAll();
        $PData = array();
        foreach ($p as $prov){
            $provinceSons = array();
            $sons = $CityModel->getSons($prov->getCode());
            foreach ($sons as $area){
                $id = $area->getCode();
                $name = $area->getName();
                $sonsArea = $AreaModel->getSons($id);
                $citySons = [];
                foreach ($sonsArea as &$row) {
                    $aid = $row->getCode();
                    $aname = $row->getName();
                    $citySons[] = array(
                        "id" => $aid,
                        "name" => $aname,
                        "sons" => []
                    );
                }

                $provinceSons[] = array(
                    "id" => $id,
                    "name" => $name,
                    "sons" => $citySons
                );
            }


            $PData[] = array(
                "id" => $prov->getCode(),
                "name" => $prov->getName(),
                "sons" => $provinceSons
            );
        }
        echo json_encode($PData);
    }

}
